/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _ZIPFILE_ZIPFILE_H
#define _ZIPFILE_ZIPFILE_H

#ifdef __cplusplus
extern "C" {
#endif


#define OK 						0
#define SOURCE_FILE_NULL_ERROR 	-1
#define EOCD_LEN_ERROR			-2
#define COMMET_LEN_ERROR    	-3
#define ENTRY_LEN_ERROR    		-4
#define ENTRY_SIGNATURE_ERROR   -5
#define EOCD_NOT_FOUND_ERROR    -6
#define ARCHIVE_ERROR    		-7


typedef struct Zipentry {
    unsigned long fileNameLength;
    const unsigned char* fileName;
    unsigned short compressionMethod;
    unsigned int uncompressedSize;
    unsigned int compressedSize;
    const unsigned char* data;
    
    struct Zipentry* next;
} Zipentry;

typedef struct Zipfile
{
    const unsigned char *buf;
    unsigned int bufsize;

    // Central directory
    unsigned short  disknum;            //mDiskNumber;
    unsigned short  diskWithCentralDir; //mDiskWithCentralDir;
    unsigned short  entryCount;         //mNumEntries;
    unsigned short  totalEntryCount;    //mTotalNumEntries;
    unsigned short  entryIndex;    		//mEntriesIndex;
    unsigned int    centralDirSize;     //mCentralDirSize;
    unsigned int    centralDirOffest;  // offset from first disk
    unsigned short  commentLen;         //mCommentLen;
    const unsigned char*  comment;       //mComment;
    const unsigned char* curEntryOffset;

	// entry
    Zipentry entries;
} Zipfile;



int zipfile_init(Zipfile* file, const void* data, size_t size, const char* fileName);
int zipfile_beging(Zipfile* file);
unsigned char zipfile_is_end(Zipfile* file);
int zipfile_advance(Zipfile* file);

// The buffer must be 1.001 times the buffer size returned
// by get_zipentry_size.  Returns nonzero on failure.
int zipfile_decompress_zipentry(Zipentry* entry, void* buf, int bufsize);

#ifdef __cplusplus
} // extern "C"
#endif

#endif // _ZIPFILE_ZIPFILE_H
